package com.ruoyi.project.system.service.impl;


import com.ruoyi.project.system.domain.QuestionStore;
import com.ruoyi.project.system.mapper.*;
import com.ruoyi.project.system.service.IWxApiService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WxApiServiceImpl implements IWxApiService {
    //课程章节
    @Autowired
    CourseCatalogMapper courseCatalogMapper;

    //课程章节内容
    @Autowired
    CourseMapper courseMapper;

    //安全知识类别
    @Autowired
    KnowledgeCatalogMapper knowledgeCatalogMapper;

    @Autowired
    QuestionStoreMapper questionStoreMapper;


    //安全知识内容
    @Autowired
    KnowledgeArticleMapper knowledgeArticleMapper;


    @Override
    public List<Map<String, Object>> getCourseList() {
        List<Map<String, Object>> list = new ArrayList<>();

        //获取全部的章节信息
        List<Map<String, Object>> catList = courseCatalogMapper.selectCouresCatalog();
        for (Map<String, Object> map : catList) {

            List<Map<String, Object>> ChildrenList = courseMapper.deleteCourseByCatalogId((Long) map.get("id"));
            if (ChildrenList.size() != 0 && ChildrenList != null) {
                map.put("childMenus", ChildrenList);
                list.add(map);
            } else {
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<QuestionStore> selectQuestionStoreByCourseCatalogId(Long courseCatalogId){
        return questionStoreMapper.selectByCourseCatalogId(courseCatalogId);
    }

    /**
     * 获取全部的类别下面的安全知识
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getknowledgeCatalogList() {


        List<Map<String, Object>> list = new ArrayList<>();

        List<Map<String, Object>> catList = knowledgeCatalogMapper.selectCatList();

        for (Map<String, Object> map : catList) {

            List<Map<String, Object>> chirldList = childrenList(map);
            //测试若没有子数据不加children会不会报错
            if (chirldList.size() != 0 && chirldList != null) {
                //查找安全常识内容
                for (Map<String, Object> resp : chirldList) {
                    resp.put("childMenus", acticleList(resp));
                }
                map.put("childMenus", chirldList);
            }

            list.add(map);
        }
        return list;

    }


    public List<Map<String, Object>> childrenList(Map<String, Object> map) {

        List<Map<String, Object>> list = new ArrayList<>();
        //  id as key,id as value,name as label

        List<Map<String, Object>> chirldList = knowledgeCatalogMapper.selectCatChildList(Integer.parseInt(map.get("id").toString()));

        if (chirldList.size() == 0 || chirldList == null) {
            return list;
        } else {
            for (Map<String, Object> childrenMap : chirldList) {

                List<Map<String, Object>> maplist = childrenList(childrenMap);
                if (maplist.size() != 0 && maplist != null) {
                    childrenMap.put("childMenus", maplist);
                }
                list.add(childrenMap);
            }
            return list;
        }
    }

    public List<Map<String, Object>> acticleList(Map<String, Object> map) {

        List<Map<String, Object>> chirldList = knowledgeArticleMapper.selectArticleList(Long.valueOf(map.get("id").toString()));
        return chirldList;
    }




}
